<template>
  <el-dialog
    class="public-dialog"
    title="修改监管方式"
    width="520"
    :modelValue="modelValue"
    @close="handleClose"
    draggable
    align-center
  >
    <div>
      <el-form
        ref="formRef"
        label-position="top"
        label-width="100px"
        :model="formData"
        class="form-components"
        :rules="rules"
      >
        <el-form-item label="监管方式" prop="regulatory_approach">
          <el-select
            v-model="formData.regulatory_approach"
            placeholder="请选择"
            clearable
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="合同号">
          <el-input v-model="formData.contract_number"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-button">
        <el-button :disabled="loading" @click="handleClose(false)">
          {{ disabled ? "关闭" : "取消" }}
        </el-button>
        <el-button
          v-if="!disabled"
          type="primary"
          :loading="loading"
          @click="handleSave"
        >
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ElMessage } from "element-plus";
import { predictionUpdateRan } from "../../api/index";
const props = defineProps({
  data: { type: Object, default: () => {} },
  modelValue: { type: Boolean, default: false },
  options: { type: Array, default: () => [] },
});
const { data, options } = toRefs(props);
watch(
  () => data.value,
  (val) => {
    if (!val) return;
    for (let key in val) {
      formData.value[key] = val[key];
    }
    console.log(formData.value);
  }
);
const emit = defineEmits(["update:modelValue", "refresh", "close"]);
const rules = {
  regulatory_approach: [
    { required: true, message: "请选择监管方式", trigger: "change" },
  ],
};
const loading = ref(false);
const disabled = ref(false);
const formData = ref({
  id:null,
  regulatory_approach:null,
  contract_number:null
});
const formRef = ref();
const handleSave = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true;
        // let _api = {
        //   add: opAdd,
        //   edit: opUpdate,
        // };
        const res = await predictionUpdateRan({
          ...formData.value,
        });
        if (res.code !== 200) {
          ElMessage.warning(res.message);
          return;
        }
        ElMessage.success("保存成功");
        handleClose(true);
      } catch (e) {
        if (e?.toString().includes(false)) {
          ElMessage.warning("请检查表单信息");
        }
      } finally {
        loading.value = false;
      }
    }
  });
};
const handleClose = async (refresh) => {
  formData.value = {}
  await nextTick();
  formRef.value.resetFields();
  emit("update:modelValue", false);
  emit("close");
  if (refresh) emit("refresh");
};
</script>
